package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.NewChatGroup;
import com.bnyy.message.bean.RequestEnterChatGroupResult;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.moudle.message.MessageHeaderHelper;

import java.util.HashMap;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/8
 **/
public class NewGroupChatAdapter extends BaseNormalListAdapter<NewChatGroup, NewGroupChatAdapter.ViewHolder> {


    public NewGroupChatAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_new_group_chat, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        NewChatGroup data = getData(position);

        holder.tvName.setText(data.getGroup_name());
        holder.tvMemberCount.setText("已有" + data.getMember_number() + "人加入讨论");
        holder.tvStatus.setText(data.getStatus_name());
        Drawable background = holder.tvStatus.getBackground();
        GradientDrawable bg;
        if (!(background instanceof GradientDrawable)) {
            bg = new GradientDrawable();
            bg.setCornerRadius(10);
        } else {
            bg = (GradientDrawable) background;
        }
        switch (data.getStatus()) {
            case 1:
                holder.tvStatus.setTextColor(Color.WHITE);
                bg.setColor(ContextCompat.getColor(mContext, R.color.green_theme));
                break;
            case 2:
                holder.tvStatus.setTextColor(ContextCompat.getColor(mContext, R.color.gray_text));
                bg.setColor(Color.parseColor("#F2F2F2"));
                break;
            case 3:
                holder.tvStatus.setTextColor(Color.WHITE);
                bg.setColor(Color.parseColor("#C3EED0"));
                break;
        }
        holder.tvStatus.setBackground(bg);
        holder.tvStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getStatus() == 1) {
                    RequestManager requestManager = RequestManager.getInstance();
                    HashMap<String, Integer> params = new HashMap<>();
                    params.put("id", data.getId());
                    requestManager.request(requestManager.mMessageRetrofitService.requestEnterGroupChat(RequestManager.getJsonRequestBody(params)),
                            new BaseObserverImpl<RequestEnterChatGroupResult>(mContext) {
                                @Override
                                public void onSuccess(RequestEnterChatGroupResult requestEnterChatGroupResult) {
                                    super.onSuccess(requestEnterChatGroupResult);
                                    if (requestEnterChatGroupResult.isEnter_group()) {
                                        remove(position);
                                    } else {
                                        data.setStatus(3);
                                        data.setStatus_name("已申请");
                                        notifyItemChanged(position);
                                    }
                                }
                            });
                }
            }
        });

        MessageHeaderHelper.setHeader(mContext, holder.rlHeaders, data.getImgs());
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        RelativeLayout rlHeaders;
        TextView tvName;
        TextView tvMemberCount;
        TextView tvStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            rlHeaders = itemView.findViewById(R.id.rl_headers);
            tvName = itemView.findViewById(R.id.tv_name);
            tvMemberCount = itemView.findViewById(R.id.tv_member_count);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }
}
